package com.lark.algorithm.study.linkedlist;

/**
 * @author btmood
 * @version 1.0
 * @apiNote
 * @since 2024-03-13 17:17
 */
public class LinkedList<E> {

    /**
     * 节点
     * 外部不需要知道链表的具体实现，所以把Node写为内部类
     */
    private class Node {
        public E e;
        private Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return "Node{" +
                    "e=" + e +
                    ", next=" + next +
                    '}';
        }
    }

    private Node dummyHead;
    int size;

    public LinkedList() {
        // 初始状态就存在一个虚拟节点
        dummyHead = new Node(null, null);
        size = 0;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 在链表索引处添加一个元素
     * 注意size处是链表尾，也是可以添加元素的
     * @param index 索引
     * @param e 元素
     */
    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("index is not illegal");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        prev.next = new Node(e, prev.next);
        size++;
    }

    /**
     * 在链表头添加元素
     * @param e 新元素
     */
    public void addFirst(E e) {
        add(0, e);
    }

    public void addLast(E e) {
        add(size, e);
    }

    // 获取索引位置的元素
    public E get(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("index is not illegal");
        }
        // 从头节点开始
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur.e;
    }

    public E getFist() {
        return get(0);
    }

    public E getLast() {
        return get(size - 1);
    }

    // 设置索引位置的元素为e
    public void set(int index, E e) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("index is not illegal");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.e = e;
    }

    // 查找链表中是否有元素e
    public boolean contains(E e) {
//        Node cur = dummyHead.next;
//        while (cur != null) {
//            if (cur.e.equals(e)) {
//                return true;
//            }
//            cur = cur.next;
//        }
        for (Node cur = dummyHead.next; cur != null; cur = cur.next) {
            if (cur.e.equals(e)) {
                return true;
            }
        }
        return false;
    }

    // 删除索引位置的节点
    public E remove(int index) {
        if (index < 0 || index >= size) {
            throw new IllegalArgumentException("index is not illegal");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;
        size--;
        return retNode.e;
    }

    // 从链表中删除第一个元素
    public E removeFirst() {
        return remove(0);
    }

    // 从链表中删除最后一个元素
    public E removeLast() {
        return remove(size - 1);
    }


    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = dummyHead.next;
        while (cur != null) {
            res.append(cur.e + "=>");
            cur = cur.next;
        }
        res.append("null");
        return res.toString();
    }
}
